<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     rwd_default
 * @copyright   Copyright (c) 2014 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<script>
    (function(){

        var eventMatchers = {
            'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
            'MouseEvents': /^(?:click|mouse(?:down|up|over|move|out))$/
        }
        var defaultOptions = {
            pointerX: 0,
            pointerY: 0,
            button: 0,
            ctrlKey: false,
            altKey: false,
            shiftKey: false,
            metaKey: false,
            bubbles: true,
            cancelable: true
        }

        Event.simulate = function(element, eventName) {
            var options = Object.extend(defaultOptions, arguments[2] || { });
            var oEvent, eventType = null;

            element = $(element);

            for (var name in eventMatchers) {
                if (eventMatchers[name].test(eventName)) { eventType = name; break; }
            }

            if (!eventType)
                throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');

            if (document.createEvent) {
                oEvent = document.createEvent(eventType);
                if (eventType == 'HTMLEvents') {
                    oEvent.initEvent(eventName, options.bubbles, options.cancelable);
                }
                else {
                    oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
                        options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
                        options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
                }
                element.dispatchEvent(oEvent);
            }
            else {
                options.clientX = options.pointerX;
                options.clientY = options.pointerY;
                oEvent = Object.extend(document.createEventObject(), options);
                element.fireEvent('on' + eventName, oEvent);
            }
            return element;
        }

        Element.addMethods({ simulate: Event.simulate });
    })()
</script>
<?php
$_product    = $this->getProduct();
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());
$_jsonConfig = $this->getJsonConfig();
$_renderers = $this->getChild('attr_renderers')->getSortedChildren();
$cwModel = Mage::getModel('colorswatch/colorswatch');
$cwHelper = Mage::helper('colorswatch');
$availabelAttributes = Mage::helper('core')->jsonDecode($_jsonConfig);
?>
<?php if ($_product->isSaleable() && count($_attributes)):?>
    <dl>
    <?php foreach($_attributes as $_attribute): ?>
        <?php
        $_rendered = false;
        foreach ($_renderers as $_rendererName):
            $_renderer = $this->getChild('attr_renderers')->getChild($_rendererName);
            if (method_exists($_renderer, 'shouldRender') && $_renderer->shouldRender($_product, $_attribute, $_jsonConfig)):
                $_renderer->setProduct($_product);
                $_renderer->setAttributeObj($_attribute);
                echo $_renderer->toHtml();
                $_rendered = true;
                break;
            endif;
        endforeach;

        if (!$_rendered):
        ?>
        <dt><label class="required"><em>*</em><?php echo $_attribute->getLabel() ?></label></dt>
        <dd<?php if ($_attribute->decoratedIsLast){?> class="last"<?php }?>>
            <div class="input-box">
                <select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select">
                    <option><?php echo $this->__('Choose an Option...') ?></option>
                </select>
            </div>
        </dd>
		<?php /* BlueCom Color swatch code	*/ ?>
		<?php if($cwHelper -> bcCheckAttributeActive($_attribute->getAttributeId())) : ?>
			<?php
                $optionsData = $availabelAttributes['attributes'][$_attribute->getAttributeId()]['options'];
				$swatData = unserialize($cwModel->load($_attribute->getAttributeId(), 'attribute_id') -> getContent());
				$colors = $swatData ? $swatData['color'] : array();
				$images = $swatData ? $swatData['image'] : array();
			?>
			<ul id="swatch-<?php echo $_attribute->getAttributeId();?>">
			<?php foreach($optionsData as $value) : ?>
					<li>
						<a href="javascript:void(0);" class="swatch-option" option-id="<?php echo $value['id'];?>">
						    <?php if($colors[$value['id']]) : ?>
                                <span style="color:#<?php echo $colors[$value['id']];?>"></span>
                            <?php else : ?>
                                <img src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA)."bc_colorswatch/".$images[$value['id']]; ?>"/>
                            <?php endif;?>
                        </a>
					</li>
            <?php endforeach;?>
			</ul>
            <script>
                jQuery(window).load(function(){
                    jQuery('#swatch-<?php echo $_attribute->getAttributeId();?> a.swatch-option').each(function(){
                        jQuery(this).click(function(){
                            jQuery(this).parent().parent().find('a').removeClass('selected');
                            jQuery(this).addClass('selected');
                            jQuery('#attribute<?php echo $_attribute->getAttributeId() ?>').val(jQuery(this).attr('option-id')).bind('change');
                            $('attribute<?php echo $_attribute->getAttributeId() ?>').simulate('change');
                        })
                    })
                })
            </script>
		<?php endif;?>
        <?php endif; ?>
    <?php endforeach; ?>
    </dl>
    <script type="text/javascript">
        var spConfig = new Product.Config(<?php echo $_jsonConfig ?>);
    </script>
    <?php echo $this->getChildHtml('after') ?>
<?php endif;?>
